package com.itsc.action;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.struts2.ServletActionContext;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.itsc.entities.Permission;
import com.itsc.entities.Role;
import com.itsc.service.RoleService;
import com.itsc.service.inter.IPermissionService;
import com.itsc.service.inter.IRoleService;
import com.itsc.util.HttpUtil;
import com.itsc.util.page.Page;
import com.itsc.util.page.PageUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

public class RoleAction implements ModelDriven<Role>{

	private IRoleService roleService;
	private IPermissionService permissionService;
	
	private List<Integer> per_id;
	
	private String searchname;// 查询条件
	
	/* 分页显示数据 */
	private int curPage = 1;// 当前页
	private int pageSize = 2;// 每页显示条数
	private int countPage;// 总页数
	
	public void setRoleService(IRoleService roleService) {
		this.roleService = roleService;
	}
	public void setPermissionService(IPermissionService permissionService) {
		this.permissionService = permissionService;
	}
	private Role model = new Role();
	@Override
	public Role getModel() {
		// TODO Auto-generated method stub
		return model;
	}
	
	public String getSearchname() {
		return searchname;
	}
	public void setSearchname(String searchname) {
		this.searchname = searchname;
	}
	public int getCurPage() {
		return curPage;
	}
	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getCountPage() {
		return countPage;
	}
	public void setCountPage(int countPage) {
		this.countPage = countPage;
	}
	public String list() throws Exception{
		int count = roleService.getCount();
		PageUtils.initParamter(count, pageSize);
		Page page = PageUtils.getPage();
		List<Role> roleList = roleService.getPage(page);
		ActionContext.getContext().put("roleList", roleList);
		ActionContext.getContext().put("page", page);
		return "list";
	}
	
	public String show() throws Exception{
		
		Role role = roleService.selectById(model.getRole_id());
		ActionContext.getContext().getValueStack().push(role);
		
		return "show";
	}
	
	public String addModel() throws Exception{
		
		List<Permission> permissionList = permissionService.selectParentidNull();
		ActionContext.getContext().put("permissionList", permissionList);
		return "insert";
	}
	
	public String checkRole() throws Exception{
		Boolean flag = roleService.checkRole(model.getRole_name());
		if(flag){
			
			ServletActionContext.getResponse().getWriter().print("1");
		}else{
			ServletActionContext.getResponse().getWriter().print("0");
		}
		
		return null;
	}
	
	public String add() throws Exception {
		
		Set<Permission> perSet = new HashSet<Permission>();
		for(int i=0;i<per_id.size();i++){
			Permission permission = permissionService.selectById(per_id.get(i));
			perSet.add(permission);
		}
		model.setPermissionSet(perSet);
		
		roleService.save(model);
		
		return "ok";
	}
	
	public String search() throws Exception {
		List<Role> list = null;
		Page page = null;
		if (searchname == null && searchname.equals("")) {
			int count = roleService.getCount();
			PageUtils.initParamter(count, pageSize);
			page = PageUtils.getPage();
			if (curPage <= 0) {
				curPage = 1;
			} else if (curPage > page.getTotalPage()) {
				curPage = page.getTotalPage();
			} else {
			}
			PageUtils.setCurrentPage(curPage);
			page = PageUtils.getPage();
			list = roleService.search(searchname, page);
		} else {
			int count = roleService.getCount(searchname);
			PageUtils.initParamter(count, pageSize);
			PageUtils.setCurrentPage(curPage);
			page = PageUtils.getPage();
			list = roleService.search(searchname, page);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", list);
		map.put("page", page);
		String js = JSON.toJSONString(map, SerializerFeature.WriteMapNullValue);
		HttpUtil.rendText(js);
		return null;
	}
	
	public List<Integer> getPer_id() {
		return per_id;
	}
	public void setPer_id(List<Integer> per_id) {
		this.per_id = per_id;
	}

}
